const { goodDetail, getUser, ruleList } = require("../../../utils/api");

// pages/goodsdetail/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsId: '',
    goodsDetail: {
      "imgurl": "",
      "name": "",
      "score": 0,
      "oriscore": 0,
      "tag": "",
      "express": '',
      "exc_count": 0,
      "place": '地址',
      "introduce": ''
    },
    userInfo: {
      mypoint: '',
      id: '',
      phone: ''
    },
    goodsImg: [],
    isJwt: false,  //是否登录 false  没登录  true 登录
    showLogin: false,
    showRule: false,
    ruleMsg: {},
    showPhone: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options && options.shangjiuid) {
      wx.setStorageSync('shangjiuid', options.shangjiuid);
      wx.setStorageSync('create_mode', 2)
    }
    let that = this
    that.setData({
      goodsId: options.goodsid
    })//赋值

    that.getPageData()
  },

  getPageData() {
    let that = this
    const jwt = wx.getStorageSync('jwt');
    if (jwt) {
      that.setData({
        isJwt: true
      })
    } else {
      that.setData({
        isJwt: false
      })
    }
    that.getGoodsDetail()

    if (that.data.isJwt) {
      that.getUserInfo()
    }
  },

  getGoodsDetail() {
    let params = {
      gid: this.data.goodsId
    }
    goodDetail(params).then((res) => {
      if (res.errcode == 0) {
        this.setData({
          goodsDetail: res.data.goods,
          goodsImg: res.data.goodsimgs
        })

      }
    })
  },


  getUserInfo() {
    getUser().then((res) => {

      if (res.errcode == 0) {
        this.setData({
          "userInfo.mypoint": res.user.score,
          "userInfo.id": res.user.id,
          "userInfo.phone": res.user.phone
        })
      }
    })
  },



  toOrder() {
    let that = this
    if (!that.data.isJwt) {
      that.setData({
        showLogin: true
      })
    } else {
      if (this.data.goodsDetail.score > this.data.userInfo.mypoint) {
        wx.showToast({
          title: '积分不足',
          icon: 'none',
        });
      }

      else {
        if (Number(this.data.goodsDetail.stock) - Number(this.data.goodsDetail.exc_count) <= 0) {
          wx.showToast({
            title: '库存不足,无法兑换',
            icon: 'none',
          });
        } else {
          if (this.data.userInfo.phone) {
            wx.navigateTo({ url: `/pages/orderpage/index?goodsid=${that.data.goodsId}` })
          } else {
            this.setData({
              showPhone: true
            })
          }


        }

      }

    }

  },
  toRule() {

    let that = this
    let params = {
      type: "5"
    }
    ruleList(params).then((res) => {
      if (res.errcode == 0) {
        that.setData({
          showRule: true,
          ruleMsg: res.data
        })

      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none',

        });

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '启蒙盲盒积分商城',
      path: `/packageA/pages/goodsdetail/index?shangjiuid=${this.data.userInfo.id}&goodsid=${this.data.goodsId}`,// 路径，传递参数到指定页面。

    }
  }
})